    //
//  MainController.m
//  Quiz
//
//  Created by AnhLH-Oriolus on 25/07/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MainController.h"
#import "MenuController.h"
#import "PlayerController.h"
#import "QuizAppDelegate.h"
#import "AddPlayerController.h"
#import "PlayGameController.h"
#import "ScoreViewController.h"
#import "AboutViewContrller.h"

@implementation MainController

@synthesize menuCtrl;

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
-(id)init{
	self = [super init];
	
	return self;
}


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	UIImageView *v = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
	[v setImage:[UIImage imageNamed:@"images/backGround.png"]];
	[v setUserInteractionEnabled:YES];
	self.view = v;
	[v release];
	
	[self initMenuViewCtrl];
	
}


-(void)initMenuViewCtrl{
	if(!menuCtrl){
		menuCtrl = [[MenuController alloc] init];
	}
	
	[self.view addSubview:menuCtrl.view];
	[self.view bringSubviewToFront:menuCtrl.view];
	//[menuCtrl release];
}

-(void)openAboutView{
	if(!aboutViewCtrl){
		aboutViewCtrl = [[AboutViewContrller alloc] init];
	}
	
	animateView(self.view, kCATransitionPush, kCATransitionFromRight, 0.3);
	
	[self.view addSubview:aboutViewCtrl.view];
}


-(void)openPlayerController{
	if(!playerCtrl){
		playerCtrl = [[PlayerController alloc] init];
	}
	
	animateView(self.view, kCATransitionPush, kCATransitionFromRight, 0.3);

	[self.view addSubview:playerCtrl.view];
	//[self.view bringSubviewToFront:playerCtrl.view];
}

-(void)addPlayerView{
	if(!addPlayerCtrl){
		addPlayerCtrl = [[AddPlayerController alloc] init];
	}
	
	animateView(self.view, kCATransitionPush, kCATransitionFromRight, 0.3);
	
	[self.view addSubview:addPlayerCtrl.view];
	//[self.view bringSubviewToFront:addPlayerCtrl.view];
}

-(void)returnPlayerCtrl{
	NSString *playerName = addPlayerCtrl.p_txfAddNewPlayer.text;
	
	[addPlayerCtrl.view removeFromSuperview];
	addPlayerCtrl = nil;
	
	animateView(self.view, kCATransitionPush, kCATransitionFromLeft, 0.3);
	
	[self.view addSubview:addPlayerCtrl.view];
	//[self.view bringSubviewToFront:addPlayerCtrl.view];
	[playerCtrl addNewPlayerWithName:playerName];
}

-(void)openMenuViewCtrl{
	animateView(self.view, kCATransitionPush, kCATransitionFromLeft, 0.3);
}

-(void)openPlayGameController:(NSString *)playerName{
	if(!playGame){
		playGame = [[PlayGameController alloc] init];
	}
	[playerCtrl.view removeFromSuperview];
	playerCtrl = nil;
	
	
	animateView(self.view, kCATransitionPush, kCATransitionFromRight, 0.3);
	
	[self.view addSubview:playGame.view];
	//[self.view bringSubviewToFront:playGame.view];
	[playGame setPlayerName:playerName];
	[playGame resetAllData];
}

-(void)goToScoreWithMode:(int)mode ofPlayerName:(NSString *)playerName andScore:(int)score{
	//mode 0 -> view
	//mode 1 -> can update
	if(!scoreViewCtrl){
		scoreViewCtrl = [[ScoreViewController alloc] init];
	}
	if(mode == 1){
		[playGame.view removeFromSuperview];
		playGame = nil;
	}
	
	animateView(self.view, kCATransitionPush, kCATransitionFromRight, 0.3);
	
	[self.view addSubview:scoreViewCtrl.view];
	
	g_AppDelegate.sName = [NSString stringWithString:playerName];
	g_AppDelegate.iScore = score;
	
	[scoreViewCtrl willUpdateWithPlayer:playerName andScore:score];
	[scoreViewCtrl loadViewWithMode:mode];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
